import  { deleOneOrderApi,takeOrderApi,updateOrderApi } from '@/api'
export function useOrderItemData(props) {

    function handleOrderAction (order,callBack = () => {}) {
        if(order.state === -1 || order.state === 4) {
            // 已取消
            uni.showModal({
                content: '确定删除该订单？',
                success: async  res => {
                    if(res.confirm) {
                        // 删除订单
                        await deleOneOrderApi(order.id)
                        uni.showToast({title: '删除成功', icon: 'none'})
                        callBack && callBack()
                        props && props.refreshData()
                    }
                }
            })
        }
        else if(order.state === 0) {
            // 待开始
            uni.showModal({
                content: '确定接单？',
                success: async  res => {
                   if(res.confirm) {
                       await takeOrderApi({ id: order.id })
                       uni.showToast({title: '接单成功', icon: 'none'})
                       callBack && callBack()
                       props && props.refreshData()
                   }
                }
            })

        }
        else if(order.state === 1) {
            uni.showModal({
                content: '确定送达？',
                success: async  res => {
                   if(res.confirm) {
                       await updateOrderApi({ id: order.id, state: 2 })
                       callBack && callBack()
                       props && props.refreshData()
                   }
                }
            })
        }
        else if(order.state === 2) {
            uni.showModal({
                content: '确定收货？',
                success: async  res => {
                   if(res.confirm) {
                       await updateOrderApi({ id: order.id, state: 3 })
                       callBack && callBack()
                       props && props.refreshData()
                   }
                }
            })
        }
    }

    return {
        handleOrderAction
    }
}
